#include "list.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void myprintf( void *a )
{
	if (a == NULL)
		printf("null\n");
	st *s = (st*)a;
	printf("a=%d, b=%s, c = %s\n", s->a, s->b, s->c);
}
int cmp( void *a, void *b)
{
	if (a == NULL || b == NULL )
		return 0;
	return a - b;
}

int test1()
{
	ListHead *head = NULL;
	head = NewListHead();

	int i =0;
	st s[10];
	for (i = 0; i<9; i++) { 
		s[i].a = i + 10;
		memset(s[i].b, 0x00, sizeof(s[i].b));
		sprintf(s[i].b, "111 %d\n", i);
		s[i].c = (char*)malloc(i+1 + 1);
		int j = 0;
		memset(s[i].c, 0x00, i + 1 + 1);
		for (j = 0; j < i+1; j++) {
			s[i].c[j] = 'A'+ i;
		}	
		InsertDataBeTheFirst(head, s + i);	
	}
	TraversalList(head, myprintf);
	
	st t;
	t.a = 12;
	t.b[0] = '\0';
	t.c = NULL;
	FindDataAndInsertBefore(head, s + 5, &t, cmp);
	TraversalList(head, myprintf);

	return 0;
}
void test2func(void *a)
{
	int *b = (int*)a;
	printf("%d ", *b);
}
int cmp2(void *a, void *b)
{
	int aa =*(int*)a;
	int bb =*(int*)b;
	return aa - bb;
}
	
int test2()
{
	ListHead *head = NULL;
	head = NewListHead();
	int i = 0;
	for ( i = 0; i < 9; i++) {
		int *b = (int*)malloc(sizeof(int));
		*b = i;
		InsertDataBeTheFirst( head, b );
		TraversalList(head, test2func);
		printf("\n");
	}
	int c = 5;
	int d = 4;
	FindDataAndInsertBefore(head, &d, &c, cmp2);
	TraversalList(head, test2func);
	printf("\n");
	int e = 199;
	FindDataAndInsertAfter(head, &c, &e, cmp2);
	TraversalList(head, test2func);
	printf("\n");
	int f = 0;
	for ( i = 0; i < 9; i++) {
		FindDataAndDeleteNode( head, &i, cmp2 ); 
		TraversalList(head, test2func);
		printf("\n");
	}
		FindDataAndDeleteNode( head, &c, cmp2 ); 
		TraversalList(head, test2func);
		printf("\n");
		FindDataAndDeleteNode( head, &e, cmp2 ); 
		TraversalList(head, test2func);
		printf("\n");
}
int main() 
{
	test2();
	return 0;
}
